package com.cdc.tools;

import android.content.Context;
import android.util.LruCache;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Tools {

    /**
     * 从assets中读取txt
     */
    public static String readFromAssets(Context context,String fileName) {
        try {
            InputStream is = context.getAssets().open(fileName);
            String text = readText(is);
            return text;
        } catch (Exception e){

        }
        return "";
    }

    /**
     * 按行读取txt
     *
     * @param is
     * @return
     * @throws Exception
     */
    private static String readText(InputStream is) throws Exception {
        InputStreamReader reader = new InputStreamReader(is);
        BufferedReader bufferedReader = new BufferedReader(reader);
        StringBuffer buffer = new StringBuffer("");
        String str;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
            buffer.append("\n");
        }
        return buffer.toString();
    }
}
